
Page({
  data: {
    activityDescription: '路线规划：让我们一起在美丽的杜鹃山展开一次令人难忘的环跑之旅！我们将沿着山脚步道启程，穿过茂密的树林，登上山顶俯瞰壮丽的景色。沿途还会经过清晨的溪流和古老的庙宇，让您感受自然与文化的交融。',
    userList: [
      { username: "用户A", avatarUrl: "path_to_avatar_A" },
      { username: "用户B", avatarUrl: "path_to_avatar_B" },
      { username: "用户C", avatarUrl: "path_to_avatar_C" },
      { username: "用户D", avatarUrl: "path_to_avatar_D" },
      { username: "用户E", avatarUrl: "path_to_avatar_E" },
      { username: "用户F", avatarUrl: "path_to_avatar_F" },
      { username: "用户G", avatarUrl: "path_to_avatar_G" },
      { username: "用户H", avatarUrl: "path_to_avatar_H" }
    ],
    longitude: 113.935324, // 深圳大学的经度
    latitude: 22.538288, // 深圳大学的纬度 
    markers: [
      {
        id: 1,
        longitude: 113.935324, // 标记点经度
        latitude: 22.538288, // 标记点纬度
        title: '深圳大学',
        iconPath: '/images/run3.gif', // 自定义标记点图标路径
        width: 100,
        height: 100
      }
    ],
    polyline: [] // 存储连线数据
  },
  goBack() {
    wx.navigateTo({
      url: '/pages/demo1/demo1'
    });
  },
  onLoad: function() {
    var that = this;

    wx.getLocation({
      type: "wgs84",
      success: function(res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        console.log("当前位置的经纬度为：" + res.latitude + ", " + res.longitude);
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        });
      }
    });
  },
  navigatetodetail1: function() {
    wx.navigateTo({
      url: '/pages/detail1/detail1',
    })
  },
  bindtapMap: function(e) {
    var that = this;
    console.log('=bindtapMap=', e);
    console.log('lat' + e.detail.latitude);
    console.log('long' + e.detail.longitude);

    // 绘制标记点
    var insert = {
      id: parseInt(that.data.markers.length) + 1,
      iconPath: "/images/run1.png",
      latitude: e.detail.latitude,
      longitude: e.detail.longitude,
      width: 30,
      height: 30
    };

    var markers_s = that.data.markers;
    markers_s.push(insert);
    that.setData({
      markers: markers_s
    });

    // 绘制连线
    var points = {
      latitude: e.detail.latitude,
      longitude: e.detail.longitude
    };

    var points_s = that.data.polyline;
    points_s.push(points);
    that.setData({
      polyline: points_s
    });
  },

  regionChange: function(e) {
    if (e.type === 'end' && e.causedBy === 'gesture') { // 只处理手势导致的地图区域变化事件
      // 获取点击位置的经纬度
      const latitude = e.detail.latitude;
      const longitude = e.detail.longitude;

      // 创建新的标记点对象
      const newMarker = {
        id: Date.now(), // 使用时间戳作为标记点的唯一id
        iconPath: "/images/run1.png", // 替换为你自己的标记点图标路径
        latitude: latitude,
        longitude: longitude,
        width: 30,
        height: 30
      };

      // 添加新的标记点到 markers 数组中
      const markers = this.data.markers.concat(newMarker);

      // 更新 markers 数据，触发页面重新渲染
      this.setData({
        markers: markers
      });
    }
  }
});